import { PayloadAction } from '@reduxjs/toolkit';
import { IHeaderTabItem } from 'src/interface';
import { State } from './initState';

export const addTabs = (state: State, action: PayloadAction<IHeaderTabItem>) => {
  const current = action.payload;
  // 如果存在不走添加逻辑
  if (state.tabList.some((item) => item.path === current.path)) return;
  state.tabList.push(action.payload);
};

export const removeTabs = (state: State, action: PayloadAction<string>) => {
  const current = action.payload;
  const index = state.tabList.findIndex((item) => item.path === current);
  if (index !== -1) {
    state.tabList.splice(index, 1);
  }
};

export const setTabs = (state: State, action: PayloadAction<IHeaderTabItem[]>) => {
  state.tabList = action.payload;
};
